nclude <iostream>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;
int main() {
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
        std::cerr << "Socket creation error" << std::endl;
        return -1;
    }

    struct sockaddr_in serv_addr;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(8080);
    inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);

    if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
        std::cerr << "Connection failed" << std::endl;
        close(sock);
        return -1;
    }

    // 发送数据
    const char* message = "Hello from client";
    send(sock, message, strlen(message), 0);

    // 接收数据
    char buffer[1024] = {0};
    ssize_t bytesRead = recv(sock, buffer, sizeof(buffer) - 1, 0);
    if (bytesRead < 0) {
        std::cerr << "Error receiving data" << std::endl;
    } else if (bytesRead == 0) {
        std::cout << "Connection closed by peer" << std::endl;
    } else {
        buffer[bytesRead] = '\0'; // 添加字符串结束符
        std::cout << "Received: " << buffer << std::endl;
    }

    close(sock);
    return 0;
}

